package com.hmy.es.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.http.HttpHost;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Getter
@Setter
@Log4j2
@ConfigurationProperties(prefix = "spring.elasticsearch")
public class EsProperties {

    private Map<String, EsProperty> datasource = new LinkedHashMap<>();

    @Deprecated
    public Map<String, List<HttpHost>> getHttpHostMap() {
        Map<String, List<HttpHost>> httpHostMap = new HashMap<>(2);
        datasource.forEach((k, v) -> {
            List<HttpHost> httpHosts = v.getHttpHost();
            if (!CollectionUtils.isEmpty(httpHosts)) {
                httpHostMap.put(k, httpHosts);
            }
        });
        return httpHostMap;
    }

    public Map<String, ElasticsearchClient> getEsClientMap(){
        Map<String, ElasticsearchClient> clientMap = new HashMap<>(2);
        datasource.forEach((k, v) -> {
            ElasticsearchClient elasticsearchClient = v.getElasticsearchClient();
            if (ObjectUtils.isNotEmpty(elasticsearchClient)) {
                clientMap.put(k, elasticsearchClient);
            }
        });
        return clientMap;
    }

}
